package top.yxqz.vagrancy.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import top.yxqz.vagrancy.entity.UserEntity;
import top.yxqz.vagrancy.service.UserEntityService;
import top.yxqz.vagrancy.utils.R;

import javax.servlet.http.HttpSession;

/**
 * @author: 余小小
 * @date: 2023/6/7
 * @description：后端管理的页面路由处理
 */
@Controller
public class AdminController {

    @Autowired
    private UserEntityService userEntityService;

    /**
     * 跳转到登录页面
     * uri: /
     *
     * @return
     */
    @GetMapping("/")
    public String goLogin() {
        return "page-login";
    }

    /**
     * 跳转到首页
     * uri: /admin
     *
     * @return
     */
    @GetMapping("/admin")
    public String goIndex() {
        return "index";
    }

    /**
     * 退出系统
     */
    @GetMapping("/logout")
    public String doLogout(HttpSession session) {
        session.invalidate(); //干掉Session
        return "redirect:/";
    }

    /**
     * 跳转去个人信息
     * uri: /admin/grxx
     */
    @GetMapping("/admin/grxx")
    public String methodName() {
        return "uc-grzx";
    }


    /**
     * 修改用户名
     * user  放的是用户修改后的信息
     * 原来的信息  在session
     *
     * @return
     */
    @PostMapping("/admin/upname")
    public String upname(UserEntity user, HttpSession session) {
        String new_userName = user.getUsername();
//获取域对象中的用户信息

        UserEntity user_old = (UserEntity) session.getAttribute("user_session");
        user_old.setUsername(new_userName);

        userEntityService.updateById(user_old);
        return  "redirect:/admin";
    }

}
